
/*
 * Class nay dung de ve menu
 * 
 * */

package bs.app.Client;

import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Constant.StaticText;
import bs.app.Main.CCanvas;
import bs.app.Main.CMidlet;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;

public class SubMenuItem {
	public int HsubMenu, ystartSubMenu, xSubmenu, xchosenMenu, ychosenMenu,
			xNameSubMenu, yNameSubMenu, wSubMenu;
	public int indexSubMenu;
	public String[] SubMenu;
	public ActionMenu[] action;
	public Command leftSoftKey, rightSoftKey, centerSoftKey;
	public Command leftMenuCommand, rightMenuCommand;
	public boolean paintMenu;
	public int[] Color = { 0xa1a6a1, 0xf1941c };
	public int[] border = { 0x090b7a, 0x201404 };
	public boolean[] isfocus = { true, false, false, false, false };
	public ChildrenMenu children;
	public static boolean isPaintChildren = false;
	public int soelement = 5;

	public SubMenuItem() {
		CRess.LoadSubMenu();
	}

	public void initCommand() {
		resetMenu();
		if (leftSoftKey == null) {
			leftSoftKey = new Command(StaticText.cmdMenu, new ActionMenu() {
				public void perform() {
					paintMenu = !paintMenu;
					String stMenu[] = { StaticText.cmdHome,
							StaticText.cmdSearch[CCanvas.langue],
							StaticText.cmdList[CCanvas.langue],
							StaticText.cmdHelp[CCanvas.langue],
							StaticText.cmdExit[CCanvas.langue] };
					ActionMenu[] action = new ActionMenu[5];
					action[0] = new ActionMenu() {
						public void perform() {
							if (CCanvas.AppPage1 == CCanvas.SaveStatePrevious) {
								CCanvas.CurrentScreen = CCanvas.AppPage1;
							} else {
								CCanvas.s_Instan.LoadingState.timeForLoading = 50;
								CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
								CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
								CCanvas.s_Instan.connect.koclearurl = 1;
								CCanvas.s_Instan.connect.cosubmenu = true;
								CCanvas.s_Instan.connect.setURL("");
								CCanvas.s_Instan.connect.RequestServerGetData();
								System.gc();
							}
						}
					};
					action[1] = new ActionMenu() {
						public void perform() {
						}
					};
					action[2] = new ActionMenu() {
						public void perform() {

						}
					};
					action[3] = new ActionMenu() {
						public void perform() {

						}
					};
					action[4] = new ActionMenu() {
						public void perform() {
							CMidlet.instanMidlet.exit();
						}
					};

					initSubMenuInfo(
							stMenu,
							action,
							setLeftCmdMenu(StaticText.cmdSelect[CCanvas.langue]),
							setRightCmdMenu(StaticText.cmdClose[CCanvas.langue]));
				}
			});
		}
	}

	public void initSubMenuInfo(String[] submenu, ActionMenu[] action, Command LeftCommand, Command RightCommand) {
		SubMenu = submenu;
		this.action = action;
		leftMenuCommand = LeftCommand;
		rightMenuCommand = RightCommand;
		xSubmenu = 0;
		xchosenMenu = 0;
		xNameSubMenu = 6;

		HsubMenu = submenu.length * 23 + 1;
		//ystartSubMenu = CCanvas.s_iHcanvas - HsubMenu - CConfig.HIEGHT_COMMNAND;
		ystartSubMenu = CCanvas.s_iHcanvas - HsubMenu - CRess.menubar.getHeight();
		wSubMenu = 92;
		ychosenMenu = ystartSubMenu;
		yNameSubMenu = ystartSubMenu + 3;
	}

	public void setRightSoftkey(Command sk) {
		rightSoftKey = sk;
	}

	public void setLeftSoftkey(Command sk) {
		leftSoftKey = sk;
	}

	public void setCenterSoftKey(Command sk) {
		centerSoftKey = sk;
	}

	public void resetMenu() {
		paintMenu = false;
		indexSubMenu = 0;
		action = null;
		leftMenuCommand = rightMenuCommand = null;
		children = null;
		isPaintChildren = false;
	}

	public void resetColorindex() {
		for (int i = 0; i < isfocus.length; i++) {
			isfocus[i] = false;
		}
		isfocus[0] = true;
		indexSubMenu = 0;
	}

	public Command setLeftCmdMenu(String captionLeft) {
		return (new Command(captionLeft, new ActionMenu() {
			public void perform() {
				onClick();
			}
		}));
	}

	public Command setRightCmdMenu(String captionRight) {
		return (new Command(captionRight, new ActionMenu() {
			public void perform() {
				CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
				resetColorindex();
				resetMenu();
			}
		}));
	}

	public void paint(Graphics g) {
		paintSubMenu(g);
		if (isPaintChildren == true && children != null) {
			children.paintChildrenMenu(g);
		}
	}

	public void paintSubMenu(Graphics g) {
		if (paintMenu == false) // paint softkey
		{
//			CCanvas.mediumfont.drawString(g, leftSoftKey.caption, 5,
//					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,
//					0xa1a6a1, 0x090b7a, true);
//			
			CCanvas.mediumfont.drawString(g, leftSoftKey.caption, 5,
					CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM,
					0xa1a6a1, 0x090b7a, true);
			
			
		} else // paint command soft key and sub menu
		{
			OptionMenu(g);
			PaintCommand(g);
		}
	}

	public void OptionMenu(Graphics g) {
		// border Menu
		g.setColor(0x8affff);
		g.drawRect(CCanvas.xTranslate + xSubmenu, ystartSubMenu, wSubMenu,SubMenu.length * 23);
		g.setColor(0x122025);
		g.fillRect(CCanvas.xTranslate + xSubmenu + 1, ystartSubMenu + 1,wSubMenu - 1, SubMenu.length * 23 - 1);
		for (int i = 1; i < SubMenu.length; i++) {
			g.setColor(0x8affff);
			g.drawLine(CCanvas.xTranslate + xSubmenu, ystartSubMenu + i * 23, CCanvas.xTranslate + xSubmenu + wSubMenu, ystartSubMenu + i* 23);
		}
	//	g.drawImage(CRess.menuchosen, CCanvas.xTranslate + xchosenMenu, ychosenMenu + indexSubMenu * CRess.menuitem.getHeight(),Graphics.TOP | Graphics.LEFT);
		if(CRess.menuchosen.getWidth() == 69){
			g.drawImage(CRess.menuchosen1, CCanvas.xTranslate + xchosenMenu, ychosenMenu + indexSubMenu * 23,Graphics.TOP | Graphics.LEFT);
		}else{
			g.drawImage(CRess.menuchosen, CCanvas.xTranslate + xchosenMenu, ychosenMenu + indexSubMenu * 23,Graphics.TOP | Graphics.LEFT);
		}
		
		
		if (indexSubMenu != SubMenu.length - 1 && indexSubMenu != 0)
//			g.drawImage(CRess.arrow, CCanvas.xTranslate + xchosenMenu + CRess.menuchosen.getWidth() - CRess.arrow.getWidth() - 6, ychosenMenu + indexSubMenu * CRess.menuitem.getHeight() + 9, Graphics.TOP | Graphics.LEFT);
			g.drawImage(CRess.arrow, CCanvas.xTranslate + xchosenMenu + 92 - 6 - 6, ychosenMenu + indexSubMenu * 23 + 9, Graphics.TOP | Graphics.LEFT);
		/*for (int i = 0; i < SubMenu.length; i++) {
			if (isfocus[i] == true)
				CCanvas.mediumfont.drawString(g, SubMenu[i], CCanvas.xTranslate + xNameSubMenu + 3, yNameSubMenu + 2 + i * CRess.menuitem.getHeight(), 0, Color[1], border[1], true);
			else
				CCanvas.mediumfont.drawString(g, SubMenu[i], CCanvas.xTranslate + xNameSubMenu + 3, yNameSubMenu + 2 + i * CRess.menuitem.getHeight(), 0, Color[0], border[0],true);
		}*/
		for (int i = 0; i < SubMenu.length; i++) {
			if (isfocus[i] == true)
				CCanvas.mediumfont.drawString(g, SubMenu[i], CCanvas.xTranslate + xNameSubMenu + 3, yNameSubMenu + 2 + i * 23, 0, Color[1], border[1], true);
			else
				CCanvas.mediumfont.drawString(g, SubMenu[i], CCanvas.xTranslate + xNameSubMenu + 3, yNameSubMenu + 2 + i * 23, 0, Color[0], border[0],true);
		}

	}

	public void PaintCommand(Graphics g) {
//		g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM); //namnv close
		if(CCanvas.s_iWcanvas > CRess.menubar.getWidth()){
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);
			for(int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas + CRess.footer_back.getWidth(); i+=CRess.footer_back.getWidth())
				g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}else{
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}
		
//	    CCanvas.mediumfont.drawString(g, leftMenuCommand.caption, 5, CCanvas.s_iHcanvas - 19, 0, 0xa1a6a1, 0x090b7a, true);
//		CCanvas.mediumfont.drawString(g, leftMenuCommand.caption, 5, CCanvas.s_iHcanvas - 19, 0, 0xa1a6a1, 0x090b7a, true);
//		
//		CCanvas.mediumfont.drawString(g, rightMenuCommand.caption, CCanvas.s_iWcanvas - CCanvas.mediumfont.stringWidth(rightMenuCommand.caption) - 1,
//				CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
//		
		
	    CCanvas.mediumfont.drawString(g, leftMenuCommand.caption, 5, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM, 0xa1a6a1, 0x090b7a, true);
		CCanvas.mediumfont.drawString(g, leftMenuCommand.caption, 5, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM, 0xa1a6a1, 0x090b7a, true);
		
		CCanvas.mediumfont.drawString(g, rightMenuCommand.caption, CCanvas.s_iWcanvas - CCanvas.mediumfont.stringWidth(rightMenuCommand.caption) - 1,
				CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM, 0xa1a6a1, 0x090b7a, true);
		
		
	}

	public void showChildrenMenu() {
		if (indexSubMenu != SubMenu.length - 1 && indexSubMenu != 0)
			isPaintChildren = true;
		if (indexSubMenu == 1) // Load data Search List
		{
			// //// Create Children Menu
			children = null;
			children = new ChildrenMenu();
			if (CCanvas.s_Instan.connect.DataListForChildrenMenu.length >= soelement) {
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement, CCanvas.s_Instan.connect.DataSearchList, indexSubMenu);
			} else {
				soelement = CCanvas.s_Instan.connect.DataSearchList.length;
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement,CCanvas.s_Instan.connect.DataSearchList, indexSubMenu);
				}
		} else if (indexSubMenu == 2) // Load data Danh Sach
		{
			children = null;
			children = new ChildrenMenu();
			if (CCanvas.s_Instan.connect.DataListForChildrenMenu.length >= soelement) {
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement, CCanvas.s_Instan.connect.DataListForChildrenMenu, indexSubMenu);
			} else {
				soelement = CCanvas.s_Instan.connect.DataSearchList.length;
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement, CCanvas.s_Instan.connect.DataListForChildrenMenu, indexSubMenu);
			}
		} else if (indexSubMenu == 3) // Load data Danh Sach
		{
			children = null;
			children = new ChildrenMenu();
			if (CCanvas.s_Instan.connect.DataListForChildrenMenu.length >= soelement) {
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement, CCanvas.s_Instan.connect.DataHelpList, indexSubMenu);
			} else {
				soelement = CCanvas.s_Instan.connect.DataHelpList.length;
				children.init(xSubmenu + wSubMenu, ychosenMenu + indexSubMenu * 23/*CRess.menuitem.getHeight()*/ - (soelement - 1) * 23,
						wSubMenu, soelement * 23, soelement, CCanvas.s_Instan.connect.DataHelpList, indexSubMenu);
			}
		}
	}

	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) { // left softkey
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) { // right softkey
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			}
			if (isPaintChildren == false) {
				if (px >= CCanvas.xTranslate + xSubmenu
						&& px < CCanvas.xTranslate + xSubmenu + wSubMenu
						&& py >= ystartSubMenu
						&& py <= ystartSubMenu + SubMenu.length * 23 - 1) {
					int flagindexsubmenu = (py - ystartSubMenu) / 23;
					if (flagindexsubmenu != indexSubMenu) {
						isfocus[indexSubMenu] = false;
						indexSubMenu = flagindexsubmenu;
						isfocus[indexSubMenu] = true;
					} else {
						CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
					}
				}
			} else if (px >= CCanvas.xTranslate + xSubmenu + wSubMenu
					&& px < CCanvas.xTranslate + xSubmenu + wSubMenu + wSubMenu)// children
			{
				children.pointerPressed(px, py);
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		if (isPaintChildren == false) {
			update();
			updateMenu(keyPressed, keyHold);
		} else {
			if (children != null)
				children.UpdateKeyPress(keyPressed, keyHold, currentAsccii);
		}
		CCanvas.resetkey(); // User KeyReleased
	}

	protected void updateMenu(boolean[] keyPressed, boolean[] keyHold) {
		if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
			if (rightMenuCommand != null && rightMenuCommand.action != null) {
				rightMenuCommand.action.perform();
			}
			keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = false;
		} else if (keyPressed[KeyConstant.KEY_UP]) {
			if (isPaintChildren == false) {
				if (isfocus[indexSubMenu] == true)
					isfocus[indexSubMenu] = false;

				indexSubMenu--;
				if (indexSubMenu < 0)
					indexSubMenu = SubMenu.length - 1;
				isfocus[indexSubMenu] = true;
			}

			if (indexSubMenu == SubMenu.length - 1)
				isPaintChildren = false;
			keyPressed[KeyConstant.KEY_UP] = false;
		} else if (keyPressed[KeyConstant.KEY_DOWN]) {
			if (isPaintChildren == false) {
				if (isfocus[indexSubMenu] == true)
					isfocus[indexSubMenu] = false;
				indexSubMenu++;
				if (indexSubMenu > SubMenu.length - 1)
					indexSubMenu = 0;
				isfocus[indexSubMenu] = true;
			}
			if (indexSubMenu == SubMenu.length - 1)
				isPaintChildren = false;
			keyPressed[KeyConstant.KEY_DOWN] = false;
		} else if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]
				|| keyPressed[KeyConstant.KEY_FIRE]) {
			if (leftMenuCommand != null && leftMenuCommand.action != null) {
				leftMenuCommand.action.perform();
			}
			showChildrenMenu();
			keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = keyPressed[KeyConstant.KEY_FIRE] = false;
		} else if (keyPressed[KeyConstant.KEY_RIGHT]) {
			showChildrenMenu();
		} else if (keyPressed[KeyConstant.KEY_LEFT]) {
			isPaintChildren = false;
		}
	}

	public void onClick() {
		if (action[indexSubMenu] != null) {
			action[indexSubMenu].perform();
		}
	}

	public void update() {

	}
}
